/**
 * @param {number[]} arr
 * @return {number[]}
 */
export var arrayRankTransform = function (arr) {
  const newArr = arr.map((item, index) => {
    return [item, index]
  })

  newArr.sort((a, b) => a[0] - b[0])
  const ans = new Array(arr.length).fill(0)
  let s
  for (let i = 0; i < newArr.length; i++) {
    const [val, idx] = newArr[i]
    if (i === 0) s = 1
    else if (val > newArr[i - 1][0]) s++
    ans[idx] = s
  }
  return ans
}

/**
 * hash map
 * @param {number[]} arr
 * @return {number[]}
 */
export var arrayRankTransform2 = function (arr) {
  const sortedArr = [...arr]
  sortedArr.sort((a, b) => a - b)
  const map = new Map()
  for (let i = 0; i < sortedArr.length; i++) {
    if (!map.has(sortedArr[i])) {
      map.set(sortedArr[i], map.size + 1)
    }
  }
  const ans = new Array(arr.length).fill(0)
  for (let i = 0; i < arr.length; i++) {
    ans[i] = map.get(arr[i])
  }
  return ans
}
